package com.yeshine.opendata.controller.admin;

import com.yeshine.opendata.core.AppException;
import com.yeshine.opendata.entity.SysDictionary;
import com.yeshine.opendata.entity.User;
import com.yeshine.opendata.service.IUserService;
import com.yeshine.opendata.utils.CommonUtil;
import com.yeshine.opendata.utils.PageResult;
import com.yeshine.opendata.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * Created by taohj on 2014/6/29.
 */

@Controller
@RequestMapping("/admin/user")
public class AdminUserController extends AbstractAdminControllerBase {


    @Autowired
    private IUserService service;


    @RequestMapping("listV")
    public String listV() {
        return "admin/user/list";
    }

    @ResponseBody
    @RequestMapping("list")
    public String lsit(
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "rows", required = false) Integer size
    ) {
        User user = new User();
        PageResult<User> pageResult = new PageResult<User>(page, size);
        pageResult.setT(user);
        Result<User> result = service.getByPage(pageResult);
        return result.getPageResult().toString();
    }


    /**
     * 新增页面
     */
    @RequestMapping("createV")
    public String createV() {
        return "admin/user/create";
    }

    @ResponseBody
    @RequestMapping("create")
    public String create(
            @RequestParam("username") String username,
            @RequestParam(value = "realname", required = false) String realname,
            @RequestParam(value = "email", required = false) String email,
            @RequestParam(value = "telephone", required = false) String telephone,
            @RequestParam(value = "sex", required = false) int sex,
            @RequestParam(value = "birthday", required = false) String birthday,
            @RequestParam(value = "image", required = false) MultipartFile image,
            @RequestParam(value = "description", required = false) String description
    ) throws IOException {


        User user = new User();
        user.setUsername(username);
        user.setRealname(realname);
        user.setEmail(email);
        user.setTelephone(telephone);
        user.setSex(new SysDictionary(sex));
        user.setBirthday(CommonUtil.dateParse(birthday));
        user.setHeadImageFile(image);
        user.setDescription(description);
        Result<User> result = service.create(user,getRequest());

        if (!result.isSuccess()) {
            throw new AppException("用户新增失败");
        }

        return result.toString();
    }


    /**
     * 修改页面
     */
    @RequestMapping(value = "modifyV")
    public String modifyView(
            @RequestParam("id") Integer id
    ) {

        User user=service.getById(id).getTarget();
        setRequestAttr("user",user);
        return "admin/user/modify";
    }


    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping(value = "modify")
    public String modify(
            @RequestParam("id") Integer id,
            @RequestParam("username") String username,
            @RequestParam(value = "realname", required = false) String realname,
            @RequestParam(value = "email", required = false) String email,
            @RequestParam(value = "telephone", required = false) String telephone,
            @RequestParam(value = "sex", required = false) Integer sex,
            @RequestParam(value = "birthday", required = false) String birthday,
            @RequestParam(value = "image", required = false) MultipartFile image,
            @RequestParam(value = "description", required = false) String description
    ) {
        User user = service.getById(id).getTarget();
        user.setUsername(username);
        user.setRealname(realname);
        user.setEmail(email);
        user.setTelephone(telephone);
        user.setSex(new SysDictionary(sex));
        user.setBirthday(CommonUtil.dateParse(birthday));
        user.setHeadImageFile(image);
        user.setDescription(description);
        Result<User> result = service.modify(user, getRequest());
        return result.toString();
    }


    @ResponseBody
    @RequestMapping("remove")
    public String remove(@RequestParam("id") Integer id) {
        User user = new User();
        user.setId(id);
        Result<User> result = service.remove(user);
        return result.toString();
    }


}
